﻿using System.ComponentModel.Composition;
using System.Configuration;
using OperationPlugins.WebApp.Applications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;

namespace OperationPlugins.WebApp.MongoDB
{
    [Export(typeof(IApplicationServerDataContext))]
    public class MongoDBApplicationServerDataContext : IApplicationServerDataContext
    {
        private const string ConnectionStringName = "OperationPlugins.WebApp.MongoDB";

        /// <summary>
        /// Gets the application server database.
        /// </summary>
        /// <returns>The application server database.</returns>
        private MongoDatabase GetApplicationServerDatabase()
        {
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[ConnectionStringName];
            if (settings == null)
            {
                throw new InvalidOperationException(string.Format("Could not find the connection string named {0} from the application configuration file.", ConnectionStringName));
            }

            MongoClient client = new MongoClient(settings.ConnectionString);
            MongoServer server = client.GetServer();
            MongoDatabase database = server.GetDatabase("OperationPlugins-appsrv");
            return database;
        }


        /// <summary>
        /// Is the application server running?
        /// </summary>
        bool IApplicationServerDataContext.IsRunning
        {
            get
            {
                return Convert.ToBoolean(GetValue("IsRunning", false.ToString()));
            }
            set
            {
                SetValue("IsRunning", value.ToString());
            }
        }




        private void SetValue(string key, string value)
        {
            MongoDatabase database = GetApplicationServerDatabase();
            MongoCollection<PropertyContainer> collection = database.GetCollection<PropertyContainer>("properties");

            PropertyContainer container = collection.FindAll().FirstOrDefault();

            if (container != null)
            {
                container.Data[key] = value;
                collection.Save(container);
            }
            else
            {
                container = new PropertyContainer();
                container.Data[key] = value;
                collection.Insert(container);
            }
        }

        private string GetValue(string key, string defaultValue)
        {
            string value = null;

            MongoDatabase database = GetApplicationServerDatabase();
            MongoCollection<PropertyContainer> collection = database.GetCollection<PropertyContainer>("properties");

            PropertyContainer container = collection.FindAll().FirstOrDefault();
            if (container != null)
            {
                if (container.Data.ContainsKey(key))
                {
                    value = container.Data[key];
                }
            }

            if (value == null)
            {
                value = defaultValue;
            }

            return value;
        }


        public class PropertyContainer
        {
            public PropertyContainer()
            {
                Data = new Dictionary<string, string>();
            }

            /// <summary>
            /// Id.
            /// </summary>
            public ObjectId Id { get; set; }

            public Dictionary<string, string> Data { get; set; } 
        }


    }
}
